package com.hnkywl.sts.dao.zjgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.zjgl.Pj;
import com.hnkywl.sts.entity.zjgl.Xyz;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 说明：
 * 包:        com.hnkywl.sts.dao.sfgl
 * 类描述:    付款审核dao类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Repository
public class XyzDao extends SimpleHibernateTemplate<Xyz, Long> {

    public Page<Xyz> findXyzList(String xyzhm,Long sfkId,Integer  xyzsflxType,Integer shztType, String gsMc,Long Ids,String khhMc,Long khhId,Integer zt,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Xyz> page = new Page<Xyz>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (xyzhm != null && !("").equals(xyzhm)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" xyzhm like ? ");
            param.add("%"+xyzhm.trim()+"%");
        }
        if (zt != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append("status = ? ");
            param.add(zt);
        }
        if (sfkId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" sfk.id = ? ");
            param.add(sfkId);
        }
        if (xyzsflxType != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" xyzsflx = ? ");
            param.add(xyzsflxType);
        }
        if (shztType != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" shzt = ? ");
            param.add(shztType);
        }
        if (Ids != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" nbgs.id = ? ");
            param.add(Ids);
        }
        if (Ids == null && gsMc!=null && !gsMc.equals("")) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" nbgs.jc like ? ");
            param.add("%" + gsMc.trim() + "%");
        }
        if (khhId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" czyh.id = ? ");
            param.add(khhId);
        }
        if (khhId == null && khhMc!=null && !khhMc.equals("")) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" czyh.qc like ? ");
            param.add("%" + khhMc.trim() + "%");
        }
        //==============记录数量===============
        hql = "select count(*) from Xyz where 1=1" + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition ;
        hql = hql +" order by dqrq desc";
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Xyz where 1=1" + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition ;
        hql = hql +" order by dqrq desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public Page<Xyz> findxyzListByIds(String xyzIds,Integer  xyzsflxType,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Xyz> page = new Page<Xyz>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (xyzsflxType != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" xyzsflx = ? ");
            param.add(xyzsflxType);
        }
        if (xyzIds != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" id in("+xyzIds+") ");
        }
        //==============记录数量===============
        hql = "select count(*) from Xyz where 1=1" ;
        if (condition.length() != 0)
            hql = hql + " and " + condition ;
        hql = hql +" order by dqrq desc";
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Xyz where 1=1" ;
        if (condition.length() != 0)
            hql = hql + " and " + condition ;
        hql = hql +" order by dqrq desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }


    /**
     * 查询未使用的信用证
     * @return
     */
    public Page<Xyz> findwsyXyzList(Integer  pjsflxType,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Xyz> page = new Page<Xyz>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (pjsflxType != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" xyzsflx = ? ");
            param.add(pjsflxType);
        }
        //==============记录数量===============
        hql = "select count(*) from Xyz where status = 1 " ;
        if (condition.length() != 0)
            hql = hql + " and " + condition ;
        hql = hql +" order by dqrq desc";
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Xyz where status = 1 "  ;
        if (condition.length() != 0)
            hql = hql + " and " + condition ;
        hql = hql +" order by dqrq desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    /**
     * 判断属性是否重复
     * @param id 信用证id
     * @param propertyName 付款审核属性名
     * @param propertyValue 付款审核属性值
     * @return
     */
    public Boolean checkUnique(Long id, String propertyName,Object propertyValue) {
        String hql;
        if (propertyValue == null)
            return false;
        if(id == null){
            //修改检验
            hql = "from Xyz where 1=1 and " + propertyValue + " = ? ";
            return this.existsQuery(hql, propertyName);
        }else{
            //新增检验
            hql = "from Xyz where 1 =1 and " + propertyValue + " = ? and id <>?";
            return this.existsQuery(hql, propertyName,id);
        }
    }

    public List<Xyz> getXyzBySFKId(Long sfkId){
        return (List<Xyz>)find(" from Xyz where sfk.id =  ? and deleted = 0  ",sfkId);
    }

}
